package top.sjydzq.www.controller.Mall;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import top.sjydzq.www.bean.Product;
import top.sjydzq.www.service.ProductService;
import top.sjydzq.www.service.CategoryService;

import java.util.List;

@Controller
@RequestMapping("/product/")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("list")
    public String list(String type, int tid, Model model) {
        List<Product> products = null;
        switch (type) {
            case "category":
                products = productService.selectAllByTid(tid);

                model.addAttribute("title", categoryService.selectById(tid).getTypenameme());

                break;
            case "allNew":
                products = productService.selectAll();

                model.addAttribute("title", "最新商品");

                break;
        }

        model.addAttribute("products", products);

        return "Mall/Product/list";
    }

    @RequestMapping("detail")
    public String detail(int id, Model model) {
        Product product = productService.selectById(id);
        String category = categoryService.selectById(product.getGoodstype_id()).getTypenameme();

        model.addAttribute("product", product);
        model.addAttribute("category", category);

        return "Mall/Product/detail";
    }
}
